<template>
  <div class="box">
    <div class="header">
      <div>
        <router-link to="/">首页</router-link>
      </div>
    </div>
    <div class="content">
      <div>
        <div>注册</div>
        <div>
          <span>昵称：</span>
          <input type="text" v-model="unickname" />
        </div>
        <div>
          <span>密码：</span>
          <input type="password" v-model="upwd" />
        </div>
        <div>
          <span>确认密码：</span>
          <input type="password" v-model="checkupwd" />
        </div>
        <div>
          <span>手机号：</span>
          <input type="text" v-model="uphone" />
        </div>
        <div>
          <span>邮箱：</span>
          <input type="text" v-model="uemail" />
        </div>
        <div>
          <span @click="reg">注册</span>
        </div>
        <div><router-link to="/login">已有账号？去登录&gt;</router-link></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      unickname: "",
      upwd: "",
      checkupwd: "",
      uphone: "",
      uemail: "",
    };
  },
  methods: {
    reg() {
      let { unickname, upwd, checkupwd, uphone, uemail } = this;
      if (unickname && upwd && checkupwd && uphone && uemail) {
        if (upwd != checkupwd) {
          this.$alert("密码前后不一致", "注册提示", {
            confirmButtonText: "确定",
          });
          return;
        }
        let regphone = /^1[3-9]\d{9}$/;
        if (!regphone.test(this.uphone)) {
          this.$alert("手机号格式错误", "注册提示", {
            confirmButtonText: "确定",
          });
          return;
        }
        let regemail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (!regemail.test(this.uemail)) {
          this.$alert("邮箱格式错误", "注册提示", {
            confirmButtonText: "确定",
          });
          return;
        }
        const url = "http://120.78.224.75:6677/register";
        const params = `unickname=${unickname}&upwd=${upwd}&uphone=${uphone}&uemail=${uemail}`;
        this.axios.post(url, params).then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            this.$alert("注册成功", "注册提示", {
              confirmButtonText: "确定",
            });
            // 更新用户名 存储到共享的vuex中
            this.$store.commit("updateUnickname", unickname);
            this.$store.commit("updateUemail", uemail);
            this.$router.push("/login");
          } else if (res.data.code == 201) {
            this.$alert("用户已存在", "注册提示", {
              confirmButtonText: "确定",
            });
          } else {
            this.$alert("服务器错误", "注册提示", {
              confirmButtonText: "确定",
            });
          }
        });
      } else {
        this.$alert("表单不允许有空", "注册提示", {
          confirmButtonText: "确定",
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  background-color: #90080e;
  height: 40px;
  > div:first-child {
    line-height: 40px;
    margin-left: 10px;
    > a {
      font-size: 16px;
      color: #fff;
    }
  }
}
.content {
  background: url("../assets/img/f1/bg2.png") no-repeat;
  height: 100%;
  width: 100%;
  background-size: cover;
  background-attachment: fixed;
  min-height: 98vh;
  display: flex;
  align-items: center;
  justify-content: center;
  > div {
    width: 700px;
    height: 420px;
    background-color: rgba(255, 255, 255, 0.9);
    border-radius: 20px;
    display: flex;
    align-items: center;
    flex-direction: column;
    padding-top: 30px;
    > div:first-child {
      font-size: 36px;
      color: #90080e;
      font-weight: bold;
      margin-bottom: 10px;
      margin-left: 40px;
    }
    > div:nth-child(2) {
      margin-bottom: 15px;
      > span {
        display: inline-block;
        width: 90px;
        font-size: 18px;
        color: black;
      }
      > input {
        width: 350px;
        height: 35px;
        border: 1px solid #aaa;
      }
    }
    > div:nth-child(3) {
      margin-bottom: 15px;
      > span {
        display: inline-block;
        width: 90px;
        font-size: 18px;
        color: black;
      }
      > input {
        width: 350px;
        height: 35px;
        border: 1px solid #aaa;
      }
    }
    > div:nth-child(4) {
      margin-bottom: 15px;
      > span {
        display: inline-block;
        width: 90px;
        font-size: 18px;
        color: black;
      }
      > input {
        width: 350px;
        height: 35px;
        border: 1px solid #aaa;
      }
    }
    > div:nth-child(5) {
      margin-bottom: 15px;
      > span {
        display: inline-block;
        width: 90px;
        font-size: 18px;
        color: black;
      }
      > input {
        width: 350px;
        height: 35px;
        border: 1px solid #aaa;
      }
    }
    > div:nth-child(6) {
      margin-bottom: 10px;
      > span {
        display: inline-block;
        width: 90px;
        font-size: 18px;
        color: black;
      }
      > input {
        width: 350px;
        height: 35px;
        border: 1px solid #aaa;
      }
    }
    > div:nth-child(7) {
      > span {
        display: inline-block;
        width: 120px;
        height: 50px;
        color: white;
        background-color: #90080e;
        line-height: 50px;
        text-align: center;
        font-size: 18px;
        border-radius: 30px;
        margin-left: 40px;
        cursor: pointer;
      }
    }
    > div:last-child {
      margin-top: 10px;
      margin-left: 40px;
      > a {
        color: black;
      }
      > a:hover {
        color: #90080e;
      }
    }
  }
}
</style>
